package me.thinkjet.controller.sso;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.thinkjet.sso.server.Credentials;
import me.thinkjet.sso.server.service.CentralAuthenticationService;
import me.thinkjet.sso.server.service.CookieService;
import me.thinkjet.sso.server.ticket.ServiceTicket;
import me.thinkjet.sso.server.ticket.TicketGrantingTicket;
import me.thinkjet.sso.server.ticket.exception.TicketException;

import com.jfinal.aop.ClearInterceptor;
import com.jfinal.aop.ClearLayer;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.jfinal.log.Logger;

@ControllerBind(controllerKey = "/login", viewPath = "login")
@ClearInterceptor(ClearLayer.ALL)
public class LoginController extends Controller {
    private static final Logger log = Logger.getLogger(LoginController.class);
    private final static String SERVICE = "service";
    private final static String REDIRECT_VIEW = "redirect.html";
    private final static String LOGIN_VIEW = "index.html";
    CentralAuthenticationService centralAuthenticationService = new CentralAuthenticationService();

    public void index() {
	final String service = this.getPara(SERVICE,
		this.getCurrentService(getRequest()));
	if (this.getRequest().getMethod().equalsIgnoreCase("get")) {
	    final String TGT_Cookie = this
		    .getCookie(TicketGrantingTicket.COOKIE_NAME);
	    if (StringKit.isBlank(TGT_Cookie)) {
		this.render(LOGIN_VIEW);
	    } else {
		try {
		    String serviceTicketId = centralAuthenticationService
			    .grantServiceTicket(TGT_Cookie, service);
		    this.setAttr(SERVICE, service);
		    this.setAttr(ServiceTicket.TICKET_NAME, serviceTicketId);
		    this.render(REDIRECT_VIEW);
		} catch (TicketException e) {
		    e.getCode();
		    e.printStackTrace();
		}
	    }

	} else if (this.getRequest().getMethod().equalsIgnoreCase("post")) {
	    Credentials credentials = this.generalCredentials(
		    this.getPara("username"), this.getPara("password"),
		    this.getParaToBoolean("remember"));
	    try {
		String ticketGrantingTicketId = centralAuthenticationService
			.createTicketGrantingTicket(credentials);
		String serviceTicketId = centralAuthenticationService
			.grantServiceTicket(ticketGrantingTicketId, service,
				credentials);
		this.general_TGT_Cookie(getResponse(), ticketGrantingTicketId,
			credentials.isRememberme());
		this.setAttr(SERVICE, service);
		this.setAttr(ServiceTicket.TICKET_NAME, serviceTicketId);
		this.render(REDIRECT_VIEW);
	    } catch (TicketException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }

	}
    }

    private Credentials generalCredentials(String username, String password,
	    Boolean rememberme) {
	Credentials credentials = new Credentials();
	credentials.setName(username);
	credentials.setPassword(password);
	credentials.setRememberme(rememberme);
	return credentials;

    }

    private void general_TGT_Cookie(HttpServletResponse response,
	    String cookieValue, Boolean rememberme) {
	CookieService cookieService = new CookieService(
		TicketGrantingTicket.COOKIE_NAME);
	cookieService.addCookie(getResponse(), cookieValue, rememberme);
    }

    private String getCurrentService(HttpServletRequest request) {
	return request.getScheme() + "://" + request.getServerName() + ":"
		+ request.getServerPort() + request.getContextPath();
    }
}
